#! /usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage; # use POD for Usage
#use Win32::OLE; # for creation of links in windows environments


########  Declarations #######
my %options=(
			os=>undef,
			sid=>undef,
			nwserver=>undef,
			client=>undef,
			dbpool=>undef,
			logpool=>undef,	  
			db=>undef,
			retent=>undef,
			nsr_path=>undef,
			perl_path=>undef,
			file_extension=>'sh', 		###  <---Research
			lowercase_sid=>undef,
			username=>undef,
			dbuser_password=>undef,
			pwd=>undef,
			#initial=>undef,    			####   	flag for init -> next version
			update=>undef,     			####   	flag for update of entries
			flag_help=>undef, 			####	declare flag for help display
			sqlname=>undef,
			named=>undef,				####	flag for named instance
			oracle_version=>undef,
			env_oracle_sid=>undef,
			SAPDATA_HOME=>undef,
			ORACLE_HOME=>undef,
			DIR_LIBRARY=>undef,			####	=SAP_BIN
			NLS_LANG=>undef,			
			SAPBACKUP=>undef,
			SAPARCH=>undef,
			SAPTRACE=>undef,
			SAPCHECK=>undef,
			SAPREORG=>undef,
			savesets=>undef,
			parallelism=>undef,
			sessions=>1,
			db2lib=>undef,
			db2_backup_string=>'',
			DB2DIR=>'',
			dbmcli_bin=>undef,
			nr_ignores_force=>undef,
			nr_ignores=>undef,
			nr_wait=>undef,
			nsr_log_file=>undef,
			win_public_path=>undef,
			max_pw=>undef,
			NSR_SERVER=>undef,
			NSR_CLIENT=>undef,
			NSR_DATA_VOLUME_POOL=>undef,
			NSR_LOG_VOLUME_POOL=>undef,
			NSR_SAVESET_BROWSE=>undef,
			NSR_SAVESET_RETENTION=>undef,
			DB2INSTANCE=>undef,
			DB2_ALIAS=>undef,
			DB2_INSTALL_PATH=>undef,
			DB2_NODE_NAME=>undef,
			DB2_OPTIONS=>undef,
			DB2_SESSIONS=>undef,
			DB2_USER=>undef,
			INSTHOME=>undef,
			USER_PSWD=>undef,
			install_path=>undef,
			dest_folder=>undef,
			res_path=>undef
			
			
			
			
);

my %read_opt=(
			database=>undef,
			os=>undef,
			sid=>undef,
			nwserver=>undef,
			client=>undef,
			dbpool=>undef,
			logpool=>undef,	  
			db=>undef,
			retent=>undef,
			nsr_path=>undef,
			perl_path=>undef,
			file_extension=>'sh', 		###  <---Research
			lowercase_sid=>undef,
			username=>undef,
			dbuser_password=>undef,
			pwd=>undef,
			#initial=>undef,    			####   	flag for init -> next version
			update=>undef,     			####   	flag for update of entries
			flag_help=>undef, 			####	declare flag for help display
			sqlname=>undef,
			named=>undef,				####	flag for named instance
			oracle_version=>undef,
			env_oracle_sid=>undef,
			SAPDATA_HOME=>undef,
			ORACLE_HOME=>undef,
			DIR_LIBRARY=>undef,			####	=SAP_BIN
			NLS_LANG=>undef,			
			SAPBACKUP=>undef,
			SAPARCH=>undef,
			SAPTRACE=>undef,
			SAPCHECK=>undef,
			SAPREORG=>undef,
			savesets=>undef,
			parallelism=>undef,
			sessions=>1,
			db2lib=>undef,
			db2_backup_string=>'',
			DB2DIR=>'',
			dbmcli_bin=>undef,
			nr_ignores_force=>undef,
			nr_ignores=>undef,
			nr_wait=>undef,
			nsr_log_file=>undef,
			win_public_path=>undef,
			max_pw=>undef,
			NSR_SERVER=>undef,
			NSR_CLIENT=>undef,
			NSR_DATA_VOLUME_POOL=>undef,
			NSR_LOG_VOLUME_POOL=>undef,
			NSR_SAVESET_BROWSE=>undef,
			NSR_SAVESET_RETENTION=>undef,
			DB2INSTANCE=>undef,
			DB2_ALIAS=>undef,
			DB2_INSTALL_PATH=>undef,
			DB2_NODE_NAME=>undef,
			DB2_OPTIONS=>undef,
			DB2_SESSIONS=>undef,
			DB2_USER=>undef,
			INSTHOME=>undef,
			USER_PSWD=>undef,
			install_path=>undef
			
			
);

my %entries;
my @env;
my @file_list;
my @file_list_new;

GetOptions("nwserver=s"=>\$options{nwserver},
	   "client=s"=>\$options{client},
	   "sid=s"=>\$options{sid},
	   "dbpool=s"=>\$options{dbpool},
	   "logpool=s"=>\$options{logpool},
	   "named"=>\$options{named},
	   "db=s"=>\$options{database},
	   "retent=s"=>\$options{retent},
	   "oracle_version=s"=>\$options{oracle_version},
	   "ora_lang=s"=>\$options{NLS_LANG},
	   "db2name=s"=>\$options{DB2INSTANCE},
	   "db2path=s"=>\$options{DB2_INSTALL_PATH},
	   "sessions=s"=>\$options{sessions},
	   "insthome=s"=>\$options{INSTHOME},
	   "help|?"=>\$options{flag_help},
	   "username=s"=>\$options{username},
	   "max_pw=s"=>\$options{max_pw},
	   "dbmcli=s"=>\$options{dbmcli_bin},
	   "nr_ignores=s"=>\$options{nr_ignores},
	   "nr_ignores_force=s"=>\$options{nr_ignores_force},
	   "nr_wait=s"=>\$options{nr_wait},
	   "dbuserpass=s"=>\$options{dbuser_password},
	   #"initial"=>\$options{initial}  -> implemented in next version
);





sub logging {
 	my $log_message = join(' ',@_); 
 	open (LOG,">>$options{nsr_log_file}") or die "could not open log file $options{nsr_log_file}\n"; # open log-file
 	print LOG localtime()." ".$log_message."\n"; # print message with timestamp into log-file
 	close (LOG) or die "could not close log file $options{nsr_log_file}\n"; # close log-file
}
### sub for Determination of OS ####  can take up to 1 Minute on windows os (windows 2012 not implemented)
sub determ_os(){
		if ($^O =~ /Win/){
		$options{os}="Win";
		$options{nsr_path}='C:/Legato/nsr';
		$options{perl_path}=`dir /S /B C:\\Nagios | findstr "perl.exe"`;
		my $os=`systeminfo | findstr /B /C:"OS Name"`;
		if ($os =~ m/s*Server 2003.s*/){
			$options{win_public_path}='C:/Documents and Settings/All Users/Desktop';
		}
		if ($os =~ m/s*Server 2008.s*/){
			$options{win_public_path}='C:/Users/Public/Desktop';
		}
	}else {$options{nsr_path}='/nsr';}
	if ($^O =~ /aix/){
		$options{os}="aix";
	}elsif ($^O =~ /linux/){
		$options{os}="linux";
	}elsif ($^O =~ /hpux/){
		$options{os}="hpux";
	}
	
	$options{install_path}="$options{nsr_path}/install";
	if (-d $options{install_path}){
		mkdir("$options{install_path}",0777);
	}
	$options{nsr_log_file}="$options{install_path}/nsr_install.log";
	if (!-e $options{nsr_log_file}){open (LOG,">$options{nsr_log_file}");close (LOG);
}
	$options{res_path}="$options{nsr_path}/res";
}
### sub for reading files (full path)
sub read_file(){
	my %args=@_;
	my $file=$args{file};
	&logging($file);
	my $folder=$args{folder};
	&logging($folder);
	chdir($folder);
	open(FILE,"<$file") or die "could not open file $file: $!"; # open config file
		my @entries=<FILE>; # read paramters from file
		chomp @entries; # cut \n
	close(FILE); # close file

	foreach (@entries){ # read each line in file
		next if ($_ =~ m/^$/ || $_ =~ m/^#/); # skip empty line or line begin with # = comment
	
		my($key,$value)=split(/\s*=\s*/,$_,2); # split line into key and value, from form key = value
		$key =~ s/\s+$//; #remove trailing spaces
		if ($value){$value =~ s/^\s+//;} #remove leading spaces
		if ($value){if ($key ne "pwd"){$value=~ s/\s*#.*//g if ($value =~ m/#/);}} # check if line contains comment # - and cutcomment only if line does not contain password
		$options{$file}{$key}=$value; # set key and value in hash
		if ($value){&logging("$key = $value");}else {&logging("$key = nix-null-komma-nuescht");}
	}
}  
### sub for reading nsr-directory
sub read_dir(){
	my %args=@_;
	my $folder=$args{folder};
	opendir(DIR, $folder);
	@file_list =  readdir(DIR);
	closedir(DIR);
	shift @file_list;
	shift @file_list;
}

### sub for writing into file
sub write_file(){
	my %args=@_;
	my $folder=$args{folder};
	my $file=$args{file};
	my $key_string=$args{key_string};
	my $value_string=$args{value_string};
	my @temp;
	
	open(FILE,"$folder/$file");
	while (<FILE>) {push @temp,$_;}
	close(FILE);
	
	foreach (@temp){
		my($key,$value)=split(/\s*=\s*/,$_,2); # split line into key and value, from form key = value
		$key =~ s/\s+$//; #remove trailing spaces
		$value =~ s/^\s+//; #remove leading spaces
		if ($key eq $key_string){$value=$value_string; $_="$key = $value\n";}
		print $_;
	}
	open(FILE,">$folder/test");
	foreach (@temp){print FILE "$_";}
	close(FILE);
	
}
############## MAIN #############
&determ_os;
&logging ("-" x 40);
$options{dest_folder}="T:/programming/templates/configs/SAP_Oracle/4.2/unix";  ##<just for testing>
&read_dir(folder=>$options{dest_folder});
foreach (@file_list){
	&read_file(file=>$_,folder=>$options{dest_folder});}
&write_file(folder=>$options{dest_folder},file=>"initSID_LOG.utl",key_string=>"savesets",value_string=>"58");
print "ferdidsch\n";

